package com.platform.sms;

import com.platform.utils.*;
import com.platform.utils.sms.SmsKingtoo;

import java.util.Date;
import java.util.Map;

/**
 * @Description: 创蓝短信发送
 * @Author: jf2
 * @CreateDate: 2018/5/25 0025 12:11
 * @Version: 1.0
 * Copyright: Copyright (c) 2018
 **/
public class SmsSendKingtooService extends SmsSendService {

    public SmsSendKingtooService(SmsSendConfig config) {
        this.smsSendConfig = config;
    }

    @Override
    public void checkConfig() {
        if (StringUtils.isNullOrEmpty(this.smsSendConfig)) {
            throw new RRException("请先配置短信平台信息");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getKtUrl())) {
            throw new RRException("请先配置短信平台发送域名");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getKtAccount())) {
            throw new RRException("请先配置短信平台账号");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getKtPassword())) {
            throw new RRException("请先配置短信平台密钥");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getCrSign())) {
            throw new RRException("请先配置短信平台签名");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getKtUserId())) {
            throw new RRException("请先配置短信平台userId");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getKtAction())) {
            throw new RRException("请先配置短信平台action");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getKtSendType())) {
            throw new RRException("请先配置短信平台SendType");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getKtCodingType())) {
            throw new RRException("请先配置短信平台CodingType");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getKtBackEncodType())) {
            throw new RRException("请先配置短信平台BackEncodType");
        }

    }

    @Override
    public String sendPost(String mobile, String content, Date stime, String extno) {
        try {
            /**
             * 状态,发送编号,无效号码数,成功提交数,黑名单数和消息，无论发送的号码是多少，一个发送请求只返回一个sendid，如果响应的状态不是“0”，则只有状态和消息
             */
            return SmsKingtoo.sendSms(smsSendConfig.getKtUrl(), smsSendConfig.getKtUserId(), smsSendConfig.getKtAccount(),DateUtils.format(stime, "yyyy-MM-dd HH:mm:ss"),
                            smsSendConfig.getKtPassword(), mobile, content, smsSendConfig.getKtSign(), smsSendConfig.getKtAction(),
                            smsSendConfig.getKtSendType(), smsSendConfig.getKtCodingType(), smsSendConfig.getKtBackEncodType());

        } catch (Exception e) {
            throw new RRException("短信发送失败");
        }
    }

    @Override
    public boolean resultParse(String result) {
        try {
            Map<String, Object> resultMap = XmlUtil.xmlStrToMap(result);
            if(result.indexOf("returnstatus") != -1 && "Success".equals(resultMap.get("returnstatus").toString())){
                //if(result.indexOf("errorstatus") != -1 )
                //发送成功
                return true;
            }else {
                //发送失败
                throw new RRException(resultMap.get("message").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException("短信发送失败");
        }
    }
}
